import {mapActions, mapState} from "vuex"
import headerWrap from "../header.vue"
import noData from "../noMoreData.vue"

const newsUrl = "";

export default {
  data(){
    return {
      type: 1,
      show: true,
      switchFlag: 0,
      pageSize: 20,
      pendingPageIndex: 1,
      completePageIndex: 1,
      pendingList: [1,2,3],
      completeList: [1,2,3],
      header: "预警管理"
    }
  },
  methods: {
    init(){
      // setTimeout(() => {
      //   this.pendingListInit();
      //   this.completeListInit();
      // }, 100);
    },
    pendingListInit() {
      window.pendingScroll = new MeScroll("pending", {
        //刷新
        down: {
          callback: () => {
            this.getList(this.type, 1, 1);
          }
        },
        //加载更多
        up: {
          page: {
            size: this.size
          },
          auto: false,
          loadFull: {
            use: true,
            delay: 500
          },
          htmlNodata: '<p class="upwarp-nodata">-- 暂无更多数据 --</p>',
          callback: () => {
            this.getList(this.type, 1, 2);
          }
        }
      });
    },
    completeListInit() {
      window.completeScroll = new MeScroll("complete", {
        //刷新
        down: {
          callback: () => {
            this.getList(this.type, 2, 1);
          }
        },
        //加载更多
        up: {
          page: {
            size: this.size
          },
          auto: false,
          loadFull: {
            use: true,
            delay: 500
          },
          htmlNodata: '<p class="upwarp-nodata">-- 暂无更多数据 --</p>',
          callback: () => {
            this.getList(this.type, 2, 2);
          }
        }
      });
    },
    getList(v, p, c){
      /*
       v：加载列表类型
       p：1：待处理 2：已完结
       c：1：刷新 2：加载更多
       */
      switch (v) {
        case 1:
          this.detailPath = "/news";
          this.newsListInit(p, c);
          break;
      }
    },
    // listFlag 1：待处理 2：已完结     scrollFlag 1：刷新 2：加载更多
    newsListInit(listFlag, scrollFlag){
      let data = {
        userID: userInfo.id,
        size: this.pageSize,
      };
      //今日快报待处理
      if (listFlag === 1) {
        data.type = 0;
        if (scrollFlag === 1) {
          //刷新待处理列表
          data.page = 1;
          this.pendingPageIndex = 1;
          ajax_.getType(newsUrl, data, (v) => {
            this.pendingList = this.newsListFormat(v.records);
            pendingScroll.endSuccess(v.records.length);
          });
        } else {
          // 加载更多
          data.page = this.pendingPageIndex++;
          ajax_.getType(newsUrl, data, (v) => {
            this.pendingList = this.pendingList.concat(this.newsListFormat(v.records));
            pendingScroll.endSuccess(v.records.length);
          });
        }
      } else {
        //今日快报完结列表
        data.type = 1;
        if (scrollFlag === 1) {
          //刷新待处理列表
          data.page = 1;
          this.completePageIndex = 1;
          ajax_.getType(newsUrl, data, (v) => {
            this.completeList = this.newsListFormat(v.records);
            completeScroll.endSuccess(v.records.length);
          });
        } else {
          // 加载更多
          data.page = this.completePageIndex++;
          ajax_.getType(newsUrl, data, (v) => {
            this.completeList = this.completeList.concat(this.newsListFormat(v.records));
            completeScroll.endSuccess(v.records.length);
          });
        }
      }
    },
    newsListFormat(arr){
      return arr.map((i) => {
        if (i.status == 0) {
          if (i.approverInfo.linkdoodID == userInfo.id) {
            i.stateClass = "i-state-pend";
            i.stateTxt = "待办";
          } else {
            i.stateClass = "i-state-done";
            i.stateTxt = "已办";
          }
        } else if (i.status == 1) {
          i.stateClass = "i-state-complete";
          i.stateTxt = "办结";
        }
        return i;
      });
    },
    go_details(item){
      // goPathWithParams("/news");
       goPathWithParams("/details");
    }
  },
  components: {
    headerWrap,
    noData,
  },
  mounted(){
    this.init()
  }
}
